package AEF::AEFSearch;

use Modern::Perl;
use Moose;

require AEF::AEFReader;
require AEF::ModuleGrabber;
require AEF::EntityGrabber;
require AEF::SourceGrabber;

has 'IgnoreNonPEFiles' => ( is => 'rw', isa => 'Bool');

sub GetVersion
{
	my $self = shift;
	my $data = shift;

	my $processor = AEF::ModuleGrabber->new(only_exes_and_dlls => $self->IgnoreNonPEFiles);
	my $aef = AEF::AEFReader->new(processor => $processor);
	$aef->ReadAEF($data);
	my @modules = grep { $_->name =~ /About/ } @{$processor->modules};
	my @entities = map { @{$aef->ReadBlocks($_->ptr, AEF::EntityGrabber->new())->entities} } @modules;
	my @refined = grep { $_->name eq "VS_VERSION_INFO" } @entities;
	if(!@refined)
	{
		# most apps have the version info in the about module.
		# if this one does't we'll have to search all the modules.
        @entities = map { @{$aef->ReadBlocks($_->ptr, AEF::EntityGrabber->new())->entities} } @{$processor->modules};
		@refined = grep { $_->name eq "VS_VERSION_INFO" } @entities;
	}
	for my $entity (@refined)
	{
        my $source = AEF::SourceGrabber->new();
        $aef->ReadBlocks($entity->ptr, $source);
		my $code = $source->source;
		$_ = $code;
		my ($version) = /PRODUCTVERSION\s*((?:\w+,?)+)/;
		if($version)
		{
			$version =~ s/,/./g;
			return $version;
		}
	}
    return "";
}

1;

no Moose;

